﻿using Common;
using Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using WpfCommCtl.Controls;

namespace WpfCommCtlTest
{
    internal class MainWindowViewModel : NotifyBase
    {
        #region LogViewTest

        private string _log;
        private static readonly object _lock = new object();

        public string Log
        {
            get { return _log; }
            set
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    _log = value;
                    this.DoNotify();
                }));
            }
        }

        public CommandBase StartCommand { get; set; }
        public CommandBase StopCommand { get; set; }

        public CommandBase UpdateLogView2Command { get; set; }

        public MainWindowViewModel()
        {
            //DM3058E.Connect("COM3");
            StartCommand = new CommandBase(Start);
            StopCommand = new CommandBase(Stop);
            UpdateLogView2Command = new CommandBase(UpdateLogView2);
            this.ImportGerberReturn += importGerberReturn;
        }

        private int logview2Index = 0;

        private void UpdateLogView2(object obj)
        {
            string tmp = $"第[{logview2Index++}]条日志";
            if (logview2Index % 2 == 0)
            {
                tmp += "!";
            }
            Log = tmp;
        }

        private void importGerberReturn(ImportGerberResult obj)
        {
            Console.WriteLine("import gerber successfully");
        }

        private void Stop(object obj)
        {
            working = false;
        }

        private bool working = true;

        private void Start(object obj)
        {
            working = true;
            Task.Run(() =>
            {
                int x = 0;
                while (working)
                {
                    //Log = ($"第{x++}条日志 threadId{Thread.CurrentThread.ManagedThreadId}");
                    Log = ($"第{x}条日志 threadId{Thread.CurrentThread.ManagedThreadId}");
                    Thread.Sleep(100);
                }
            });
            //Task.Run(() =>
            //{
            //    int y = 0;
            //    while (working)
            //    {
            //        Log = ($"{y++} threadId{Thread.CurrentThread.ManagedThreadId}");
            //        Thread.Sleep(100);
            //    }
            //});
        }

        #endregion LogViewTest

        #region Gerber处理

        private Action<ImportGerberResult> ImportGerberReturn;

        #endregion Gerber处理
    }
}